package com.zhongkai.tools;

import java.net.MalformedURLException;
import java.net.URL;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts.util.MessageResources;

public class DomainTool {
	Logger log = Logger.getLogger("DomainTool");
	private static MessageResources messages= MessageResources.getMessageResources("ApplicationResources");		

	public static String getDomainName(HttpServletRequest request)
			throws MalformedURLException {
		URL url = null;
		url = new URL(request.getRequestURL().toString());
		return url.getHost();
	}
	
	 public static String  getRemoteAddr(HttpServletRequest request)
	 {
	   String ip = request.getHeader("x-forwarded-for");
	   if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
	   {
	     ip = request.getHeader("Proxy-Client-IP");
	   }
	   if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
	   {
	     ip = request.getHeader("WL-Proxy-Client-IP");
	   }
	   if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
	   {
	     ip = request.getRemoteAddr();
	   }	  
	   return ip;
	}
	 
	 public static String getI18NKey(HttpServletRequest request, String key)
	 {
		 String info="";
		 if(messages!=null)			
			 info = messages.getMessage(request.getLocale(),key);		
		 return info;
	 }
}
